package com.csu.jpetstore_cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csu.jpetstore_cms.common.CommonResponse;
import com.csu.jpetstore_cms.entity.Merchant;
import com.csu.jpetstore_cms.service.MerchantService;
import com.csu.jpetstore_cms.mapper.MerchantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 朱嘉辉
* @description 针对表【merchant】的数据库操作Service实现
* @createDate 2024-03-30 10:37:52
*/
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant>
    implements MerchantService{
    @Autowired
    private MerchantMapper merchantMapper;

    /**
     * 添加商家
     */
    public CommonResponse<Merchant> addNewMerchant(Merchant merchant) {
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_account",merchant.getUsername());
        if(merchantMapper.selectOne(queryWrapper) != null){
            return CommonResponse.createForError("该账号已存在");
        }

        int result = merchantMapper.insert(merchant);
        if(result == 1){
            return CommonResponse.createForSuccess("添加成功",merchant);
        }else {
            return CommonResponse.createForError("服务器错误");
        }
    }
    /**
     * 登录
     */
    public CommonResponse<Merchant> login(String account, String password) {
        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_account",account);
        queryWrapper.eq("password",password);
        Merchant merchant = merchantMapper.selectOne(queryWrapper);
        if(merchant == null){
            return CommonResponse.createForError("账号或密码错误");
        }else {
            return CommonResponse.createForSuccess("登录成功",merchant);
        }

    }
    /**
     * 获取所有商家
     */
    public CommonResponse<List<Merchant>> getAllMerchants() {
        //todo:处理分页
        List<Merchant> merchants = merchantMapper.selectList(null);
        return CommonResponse.createForSuccess("获取成功",merchants);
    }

}




